package net.libra.utils
{
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.InteractiveObject;
	import flash.filters.BitmapFilterQuality;
	import flash.filters.ColorMatrixFilter;
	import flash.filters.GlowFilter;
	import flash.geom.ColorTransform;
	import flash.net.LocalConnection;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.utils.ByteArray;

	public class DisplayUtil
	{
		public function DisplayUtil()
		{
		}
		/**
		 * 是否前者是后者的孩子 
		 * @param display
		 * @param container
		 * @return 
		 * 
		 */		
		public static function isMyChild(display:DisplayObject,container:DisplayObjectContainer):Boolean
		{
			if(container.contains(display))
			{
				return true;
			}
			return false;
		}
		/**
		 * 得到所有孩子数组 
		 * @param container
		 * @return 
		 * 
		 */		
		public static function getChildren(container:DisplayObjectContainer):Array
		{
			var result:Array = new Array();
			for(var i:int = 0; i < container.numChildren; i++)
			{
				result.push(container.getChildAt(i));
			}
			return result;
		}
		public static function getMaxWidth(container:DisplayObjectContainer):Number
		{
			if(!container) return 0;
			var result:Number = 0;
			for(var i:int = 0; i < container.numChildren; i++)
			{
				if(result < container.getChildAt(i).width){
					result = container.getChildAt(i).width;
				}
			}
			return result;
		}
		public static function getTotalHeight(container:DisplayObjectContainer):Number
		{
			var result:Number = 0;
			for(var i:int = 0; i < container.numChildren; i++)
			{
				if(result < container.getChildAt(i).height){
					result += container.getChildAt(i).height;
				}
			}
			return result;
		}
		public static function gc():void
		{
			try
			{
				new LocalConnection().connect("gc");
				new LocalConnection().connect("gc");
			}
			catch(e:Error)
			{
				trace("error");
			}
		}
		public static function objClone(obj:Object):*{
			var byte:ByteArray = new ByteArray();
			byte.writeObject(obj);
			byte.position = 0;
			return(byte.readObject());
		}
		public static function getDeth(obj:DisplayObject):int
		{
			return obj.parent.getChildIndex(obj);
		}
	
		static public function setTextBold(tf:TextField):void
		{
			var textFormat:TextFormat = tf.defaultTextFormat;
			textFormat.bold = true;
			tf.defaultTextFormat = textFormat;
		}
		/**
		 * 递归移除孩子 
		 * @param parent
		 * @param sun
		 * 
		 */		
		static public function removeAllChildrenDigui(parent:DisplayObjectContainer,sun:Boolean=false):void
		{
			if(!parent)return;
			while(parent.numChildren>0)
			{
				var p:DisplayObjectContainer = parent.removeChildAt(0) as DisplayObjectContainer;
				if(sun && p)removeAllChildrenDigui(p);
			}
		}
		/**
		 * 移除第一层孩子 
		 * @param obj
		 * 
		 */		
		public static function removeAllChildren(obj:DisplayObjectContainer):void
		{
			while(obj.numChildren){
				obj.removeChildAt(0);
			}
		}
		/**
		 * 创建ColorMatrix数组
		 * @param brightness	亮度
		 * @param contrast		对比度
		 * @param hue			色相
		 * @param saturation	饱和度
		 */	
		static public function createColorMatrix(brightness:Number=0,contrast:Number=0,hue:Number=0,saturation:Number=0):Array
		{
			var key:String = brightness+"_"+contrast+"_"+hue+"_"+saturation;
			if(!_colors[key])
			{
				var _color:AdjustColor = new AdjustColor();
				_color.brightness = brightness;//滤镜的亮度
				_color.contrast = contrast;//滤镜的对比度
				_color.hue = hue;//滤镜的色相
				_color.saturation = saturation;//滤镜的饱和度
				_colors[key] = _color.CalculateFinalFlatArray()
			}
			return _colors[key];
		}
		static private var _colors:Object = {};
		
		
		
		public static function getFilter(color:uint):GlowFilter{
			var alpha:Number = 1;
			var blurX:Number = 3;
			var blurY:Number = 3;
			var strength:Number = 15;
			var inner:Boolean = false;
			var knockout:Boolean = false;
			var quality:Number = BitmapFilterQuality.MEDIUM;
			
			return new GlowFilter(color,
				alpha,
				blurX,
				blurY,
				strength,
				quality,
				inner,
				knockout);
		}
		
		static private var colorTransform:ColorTransform = new ColorTransform(0.5,0.5,0.5,1,65,42,33,0);
		static public function setEnabled(target:InteractiveObject,enabled:Boolean=true):void
		{
			target.mouseEnabled = enabled;
			if(target is DisplayObjectContainer)(target as DisplayObjectContainer).mouseChildren = enabled;
			if(!enabled)
			{
				//target.transform.colorTransform = colorTransform;
				target.filters = [new ColorMatrixFilter([0.4468800127506256,0.4875199794769287,0.06560000032186508,0,0,0.24688000977039337,0.6875199675559998,0.06560000032186508,0,0,0.24688000977039337,0.4875199794769287,0.2655999958515167,0,0,0,0,0,1,0])];
			}else
			{
				//target.transform.colorTransform = new ColorTransform();
				target.filters = [];
			}
		}
	}
}